package com.activity;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.util.R;
import com.util.ToastUtil;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTouch;

/**
 * Created by Administrator on 2016/4/9.
 */
public class SlidingViewActivity extends BaseActivity {
    @Bind(R.id.bt_slide)
    Button bt_slide;
    @Bind(R.id.rl_slideView)
    RelativeLayout rl_slide;
    private int mLastX;
    private int mLastY;
    private boolean isMove = false;
    private int leftOriginalValue; //view左顶点x坐标初始值
    private int topOriginalValue; //view左顶点y坐标初始值
    private int scaledTouchSlop; //最小滑动距离

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sliding_view);
        ButterKnife.bind(this);
        scaledTouchSlop = ViewConfiguration.get(SlidingViewActivity.this).getScaledTouchSlop()/2;
    }

    @OnClick(R.id.bt_slide)
    public void onSlideClick() {
        if (!isMove) {
            ToastUtil.showBottomToast("我是滑动按钮");
        } else {
            isMove = false;
        }
    }

    @OnTouch(R.id.bt_slide)
    public boolean onSlideTouch(View view, MotionEvent event) {
        if (bt_slide.getMeasuredWidth() == 0 || bt_slide.getMeasuredHeight() == 0 || rl_slide.getMeasuredWidth() == 0 || rl_slide.getMeasuredHeight() == 0) {
            return true;
        }
        int x = (int) event.getRawX();
        int y = (int) event.getRawY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mLastX = x;
                mLastY = y;
                leftOriginalValue = view.getLeft();
                topOriginalValue = view.getTop();
                break;
            case MotionEvent.ACTION_MOVE:
                int deltaX = x - mLastX;
                int deltaY = y - mLastY;
                int newLeft = view.getLeft() + deltaX;
                int newTop = view.getTop() + deltaY;
                int newRight = view.getRight() + deltaX;
                int newBottom = view.getBottom() + deltaY;
                if (newLeft < 0) {
                    newLeft = 0;
                    newRight = view.getMeasuredWidth();
                }
                if (newTop < 0) {
                    newTop = 0;
                    newBottom = view.getMeasuredHeight();
                }
                //经过实验若使用屏幕高度做判断有误差，因此使用view的父布局宽高进行判断
                if (newRight > rl_slide.getMeasuredWidth()) {
                    newRight = rl_slide.getMeasuredWidth();
                    newLeft = rl_slide.getMeasuredWidth() - view.getMeasuredWidth();
                }
                if (newBottom > rl_slide.getMeasuredHeight()) {
                    newBottom = rl_slide.getMeasuredHeight();
                    newTop = rl_slide.getMeasuredHeight() - view.getMeasuredHeight();
                }
                view.layout(newLeft, newTop, newRight, newBottom);
//                final ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) bt_slide.getLayoutParams();
//                params.leftMargin += deltaX;
//                params.topMargin += deltaY;
//                if (params.leftMargin < 0) {
//                    LogUtil.i("MyTestAdapter", "leftOut");
//                    params.leftMargin = 0;
//                }
//                if ((params.leftMargin + bt_with) > ScreenUtil.getScreenWidth()) {
//                    LogUtil.i("MyTestAdapter", "rightOut");
//                    params.leftMargin = ScreenUtil.getScreenWidth() - bt_with;
//                }
//                if (params.topMargin < 0) {
//                    LogUtil.i("MyTestAdapter", "topOut");
//                    params.topMargin = 0;
//                }
//                if (bt_slide.getMeasuredHeight() < bt_height) {
//                    params.topMargin = ScreenUtil.getScreenHeight() - bt_height;
//                    bt_slide.requestLayout();
//                    params.topMargin-=20;
//                    bt_slide.requestLayout();
//                    LogUtil.i("MyTestAdapter",bt_slide.getMeasuredHeight());
//                } else {
//                    bt_slide.requestLayout();
//                }
                mLastX = x;
                mLastY = y;
                break;
            case MotionEvent.ACTION_UP:
                //根据滑动的距离判断用户是否在滑动
                if (Math.abs(view.getLeft() - leftOriginalValue) > scaledTouchSlop || Math.abs(view.getTop() - topOriginalValue) > scaledTouchSlop) {
                    isMove = true;
                } else {
                    isMove = false;
                }
                break;
            default:
                break;
        }
        return false;
    }

}
